/*
 * DataReader.cpp
 *
 *  Created on: Oct 1, 2010
 *      Author: blyth
 */

#include "DataReader.h"
#include "dataForm.h"
#include "TH2D.h"
#include "TFile.h"
#include "TBranch.h"
#include <sstream>
#include <cstring>
#include <iostream>
#include "globals.hh"
using namespace std;

DataReader::DataReader(char* path) {
	file = new TFile(path);
}

DataReader::~DataReader() {
	file->Close();
}

bool DataReader::Read(TH2D* hist,int kn,int pn)
{
	stringstream ss;
	ss << "kaonNumber" << kn;
	TTree* tree = (TTree*)file->Get((ss.str()).c_str());
	if(!tree)
	{
		cout << "Bad kaon value\n";
		return false;
	}

	dataForm* data = new dataForm;

	stringstream ss1;
	ss1 << "pionNumber" << pn;
	TBranch* branch = tree->GetBranch((ss1.str()).c_str());
	branch->SetAddress(data);

	int count = branch->GetEntryNumber();
	for(int j = 0;j < count;j++)
	{
		branch->GetEntry(j);
		hist->Fill(data->x/m,data->y/m,data->energy/MeV);
	}

	delete data;
	delete tree;

	return true;
}
